package cn.telling.tianlianproduct.freightrules.service.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import sun.swing.StringUIClientPropertyKey;
import cn.telling.tianlianproduct.controls.vo.ProductSortVo;
import cn.telling.tianlianproduct.common.util.TianlianConstance;
import cn.telling.tianlianproduct.controls.dao.ProductSortMapper;
import cn.telling.tianlianproduct.freightrules.dao.AreaFreightRulesMapper;
import cn.telling.tianlianproduct.freightrules.dao.CarriersMapper;
import cn.telling.tianlianproduct.freightrules.dao.ProductFreightRulesAreaMapper;
import cn.telling.tianlianproduct.freightrules.dao.ProductFreightRulesMapper;
import cn.telling.tianlianproduct.freightrules.dao.ShopExpressMapper;
import cn.telling.tianlianproduct.freightrules.dao.ShopFreightRulesMapper;
import cn.telling.tianlianproduct.freightrules.dao.ShopInfoMapper;
import cn.telling.tianlianproduct.freightrules.dao.SupplyInfoMapper;
import cn.telling.tianlianproduct.freightrules.service.IFreightRulesService;
import cn.telling.tianlianproduct.freightrules.vo.AreaFreightRulesVo;
import cn.telling.tianlianproduct.freightrules.vo.CarriersVo;
import cn.telling.tianlianproduct.freightrules.vo.FreightConditionVo;
import cn.telling.tianlianproduct.freightrules.vo.ProductFreightRulesAreaVo;
import cn.telling.tianlianproduct.freightrules.vo.ProductFreightRulesVo;
import cn.telling.tianlianproduct.freightrules.vo.ShopFreightRulesVo;
import cn.telling.tianlianproduct.freightrules.vo.ShopInfoVo;
import cn.telling.tianlianproduct.otherservice.vo.SupplyVo;
import cn.telling.tianlianproduct.productrelease.dao.SupplyArea0Mapper;
import cn.telling.tianlianproduct.productrelease.service.ICommon;
import cn.telling.utils.StringHelperTools;
@Service("freightRulesService")
public class FreightRulesServiceImpl implements IFreightRulesService{
	@Autowired
	@Qualifier("carriersMapper")
	private CarriersMapper carriersMapper;
	
	@Autowired
	@Qualifier("shopInfoMapper")
	private ShopInfoMapper shopInfoMapper;
	
	@Autowired
	@Qualifier("shopFreightRulesMapper")
	private ShopFreightRulesMapper shopFreightRulesMapper;
	
	@Autowired
	@Qualifier("areaFreightRulesMapper")
	private AreaFreightRulesMapper areaFreightRulesMapper;
	
	@Autowired
	@Qualifier("supplyInfoMapper")
	private SupplyInfoMapper supplyInfoMapper;
	
	@Autowired
	@Qualifier("productSortMapper")
	private ProductSortMapper productSortMapper;
	
/*	@Autowired
	@Qualifier("areainfoMapper")
	private AreainfoMapper areainfoMapper;*/
	
	@Autowired
	@Qualifier("shopExpressMapper")
	private ShopExpressMapper shopExpressMapper;
	
	@Autowired
	private SupplyArea0Mapper supplyArea0Mapper;
	
	@Autowired
	private ProductFreightRulesAreaMapper productFreightRulesAreaMapper;
	
	@Autowired
	@Qualifier("productFreightRulesMapper")
	private  ProductFreightRulesMapper productFreightRulesMapper;
	
	@Autowired
	private ICommon commonService;
	
	@Override
	public List<CarriersVo> getShopExpressList(String shopId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("shopId", shopId);
		return carriersMapper.selectShopExpressCarrier(params);
	}

	@Override
	public ShopInfoVo getShopInfoByUserid(BigDecimal userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("selleruserid", userId);
		return shopInfoMapper.selectShopInfo(params);
	}

	public Map<String, List<ShopFreightRulesVo>> getNewShopFreightRulesByShopId(String shopid,String rulesType) {

		List<ShopFreightRulesVo> resultlist = new ArrayList<ShopFreightRulesVo>();
		//装载返回参数
		Map<String, List<ShopFreightRulesVo>> map = new HashMap<String, List<ShopFreightRulesVo>>();
		//参数设置
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("shopId", shopid);
		params.put("status", "");
		if(!"".equals(rulesType.trim())){
			//店铺规则查询
			params.put("rulesType", "".equals(rulesType.trim())?null:rulesType.trim());
			List<ShopFreightRulesVo> nowlist = shopFreightRulesMapper.selectShopExpressCarrier(params);
			map.put("allList", getShopRulesDetils(nowlist, resultlist,params));
		}else{
			params.put("rulesType", TianlianConstance.SHOP_RULES_TYPE_1);
			List<ShopFreightRulesVo> nowlist1 = shopFreightRulesMapper.selectShopExpressCarrier(params);
			map.put("oneList", getShopRulesDetils(nowlist1, resultlist,params));
			
			params.put("rulesType", TianlianConstance.SHOP_RULES_TYPE_2);
			List<ShopFreightRulesVo> nowlist2 = shopFreightRulesMapper.selectShopExpressCarrier(params);
			map.put("twoList", getShopRulesDetils(nowlist2, resultlist,params));
		}
		
		return map;
	}

	/**
	 * 
	* <p>Title:getShopRulesDetils</p>
	* <p>Description:运费规则数据处理内部类 </p>
	* <p>Company:tianlian </p>
	* @author :wxl
	* @date :2015年10月10日下午3:17:51
	* @return:List<ShopFreightRulesVo>
	 */
	private List<ShopFreightRulesVo> getShopRulesDetils(List<ShopFreightRulesVo> nowlist,List<ShopFreightRulesVo> resultlist,Map<String, Object> params){
		if (nowlist != null && nowlist.size() > 0) {
			if (!"1".equals(nowlist.get(0).getType())) {
				ShopFreightRulesVo vo = new ShopFreightRulesVo();
				vo.setType("1");
				vo.setCheckflag("f");
				nowlist.add(0, vo);
			}

			if ((nowlist.size() > 1 && !"2".equals(nowlist.get(1).getType())) || nowlist
					.size() == 1) {
				ShopFreightRulesVo vo1 = new ShopFreightRulesVo();
				vo1.setType("2");
				vo1.setCheckflag("f");
				nowlist.add(1, vo1);
			}

			if ((nowlist.size() > 2 && !"3".equals(nowlist.get(2).getType())) || nowlist
					.size() == 2) {
				ShopFreightRulesVo vo2 = new ShopFreightRulesVo();
				vo2.setType("3");
				vo2.setCheckflag("f");
				nowlist.add(2, vo2);
			}
			if (!"5".equals(nowlist.get(nowlist.size()-1).getType())) {
				ShopFreightRulesVo vo3 = new ShopFreightRulesVo();
				vo3.setType("5");
				vo3.setCheckflag("f");
				nowlist.add(vo3);
			}
			resultlist = nowlist;
		} else {
			ShopFreightRulesVo vo = new ShopFreightRulesVo();
			vo.setType("1");
			vo.setCheckflag("f");
			resultlist.add(vo);
			ShopFreightRulesVo vo1 = new ShopFreightRulesVo();
			vo1.setType("2");
			vo1.setCheckflag("f");
			resultlist.add(vo1);
			ShopFreightRulesVo vo2 = new ShopFreightRulesVo();
			vo2.setType("3");
			vo2.setCheckflag("f");
			resultlist.add(vo2);
			ShopFreightRulesVo vo3 = new ShopFreightRulesVo();
			vo3.setType("5");
			vo3.setCheckflag("f");
			resultlist.add(vo3);
		}

		for(int i=0;i<resultlist.size();i++){
			String freightid = StringHelperTools.nvl(resultlist.get(i).getId());
			params.put("freightid", freightid);
			if(!"".equals(freightid)){
				List<AreaFreightRulesVo> arealist = this.getAreaFreightRules(params);
				if(arealist!=null && arealist.size()>0){
					resultlist.get(i).setAreaFreightList(arealist);
				}
			}
		}
		return resultlist;
	}
	
	/**
	 * 
	* <p>Title:getAreaFreightRules</p>
	* <p>Description:运费规则地区数据处理内部类 </p>
	* <p>Company:tianlian </p>
	* @author :wxl
	* @date :2015年10月10日下午3:19:20
	* @return:List<AreaFreightRulesVo>
	 */
	private List<AreaFreightRulesVo> getAreaFreightRules(Map<String, Object> params){
		List<AreaFreightRulesVo> arealist;
		
		if(StringUtils.isNotBlank(params.get("status").toString().trim())){
			arealist = productFreightRulesAreaMapper.selectAreaFreightRulesByFreightid(params);
		}else{
			arealist = areaFreightRulesMapper.selectAreaFreightRulesByFreightid(params);
		}
		
		return arealist;
	}
	
	
	@Override
	public List<SupplyVo> getShopSupplyType(String shopId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("shopId", shopId);
		return supplyInfoMapper.selectsupplyShopInfoByShopId(params);
	}

	@Transactional(value = "txManager", isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public void insertNewShopFreightRules(ShopInfoVo vo,
			List<ShopFreightRulesVo> ls) {
		// 删除存在的运费规则
		shopFreightRulesMapper.deleteShopFreightRules(vo);//删除已有的运费规则
		areaFreightRulesMapper.deleteAreaFreightRules(vo);//删除已有的运费规则
		List<AreaFreightRulesVo> areals = new ArrayList<AreaFreightRulesVo>();
		for(int i=0;i<ls.size();i++){
			String id = UUID.randomUUID().toString().replaceAll("-", "");
			ls.get(i).setId(id);
			String[] a = ls.get(i).getAreaids().split(",");
			ls.get(i).setSort(new BigDecimal(i));
			for(int j=0;j<a.length;j++){
				AreaFreightRulesVo asvo = new AreaFreightRulesVo();
				asvo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
				asvo.setAreaid(new BigDecimal(a[j]));
				asvo.setFreightid(id);
				asvo.setShopId(vo.getId());
				areals.add(asvo);
			}
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		//店铺区域运费规则插入数据
		params.put("areals", areals);
		areaFreightRulesMapper.insertAreaFreightRules(params);
		//店铺运费规则插入数据
		params.put("ls", ls);
		shopFreightRulesMapper.insertNewShopFreightRules(params);
		// 更新店铺物流承诺
		//shopInfoMapper.updateShopInfoPromise(vo);
	}

	@Transactional(value = "txManager", isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public void insertNewShopFreightRule(ShopInfoVo vo,
			List<CarriersVo> sels) {
		//删除存在的承运商
		shopExpressMapper.deleteShopExpress(vo);//CarriersVo
		//店铺承运商插入数据
		for (int i = 0; i < sels.size(); i++) {
			CarriersVo vos = sels.get(i);
			vos.setShopExpressId(UUID.randomUUID().toString().replaceAll("-", "").trim());
		}
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("sels", sels);
		
		shopExpressMapper.insertShopExpress(params);
		
		
	}

	@Transactional(value = "txManager", isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public Boolean insertShopFreightRules(List<ProductFreightRulesVo> ls,String userId) {
		//查询店铺信息
		Map<String, Object> params = new HashMap<String, Object>();

		//处理数据
		List<ProductFreightRulesAreaVo> areals = new ArrayList<ProductFreightRulesAreaVo>();
		//用于存储返回值  记录id
		StringBuffer str = new StringBuffer();
		String supplyArea0Id = "";
		for(int i=0;i<ls.size();i++){
			if(!"".equals(ls.get(i).getAreaids())){
				String[] a = ls.get(i).getAreaids().split(",");
				//params.put("supplyArea0Id", ls.get(i).getSupplyArea0Id());
				//productFreightRulesMapper.deleteShopFreightRules(params);//删除已有的运费规则
				//productFreightRulesAreaMapper.deleteAreaFreightRules(params);//删除已有的运费规则
				if(supplyArea0Id==null || supplyArea0Id==""){
					supplyArea0Id = ls.get(i).getSupplyArea0Id();
				}else{
					supplyArea0Id = supplyArea0Id + "," + ls.get(i).getSupplyArea0Id();
				}
				
				String id = UUID.randomUUID().toString().replaceAll("-", "");
				str.append(id);
				ls.get(i).setId(id);
				ls.get(i).setUserid(userId);
				ls.get(i).setAddUser(new BigDecimal(userId));
				ls.get(i).setSort(new BigDecimal(i));
				ls.get(i).setRoleprice(ls.get(i).getRoleprice()==null?"":ls.get(i).getRoleprice());
				
				for(int j=0;j<a.length;j++){
					ProductFreightRulesAreaVo asvo = new ProductFreightRulesAreaVo();
					asvo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
					asvo.setAreaid(new BigDecimal(a[j]));
					asvo.setFreightid(id);
					asvo.setSupplyArea0Id(id);
					asvo.setAddUser(new BigDecimal(userId));
					areals.add(asvo);
				}
			}
		
		}
		/* 批量删除 */
		params.put("supplyArea0Id", supplyArea0Id );
		productFreightRulesMapper.deleteShopFreightRules(params);//删除已有的运费规则
		productFreightRulesAreaMapper.deleteAreaFreightRules(params);//删除已有的运费规则
				
		/*List<String> list = new ArrayList<String>();
		String[] ss = supplyArea0Id.split(",");
		for(int j=0;j<=ss.length;j++){
			list.add(ss[j]);
		}
		if(list.size()<=1000){
			params.put("supplyArea0Id", supplyArea0Id );
			productFreightRulesMapper.deleteShopFreightRules(params);
			productFreightRulesAreaMapper.deleteAreaFreightRules(params);
		}else{
			for(int m=0;(m*1000)<list.size();m++){
				supplyArea0Id = "";
				if((m+1)*1000<list.size()){
					for(int n=(m*1000);n<((m+1)*1000);n++){
						if(supplyArea0Id==null || supplyArea0Id==""){
							supplyArea0Id = list.get(n);
						}else{
							supplyArea0Id = supplyArea0Id + "," + list.get(n);
						}
					}
					params.put("supplyArea0Id", supplyArea0Id );
					productFreightRulesMapper.deleteShopFreightRules(params);
					productFreightRulesAreaMapper.deleteAreaFreightRules(params);
				}else{
					for(int n=(m*1000);n<list.size();n++){
						if(supplyArea0Id==null || supplyArea0Id==""){
							supplyArea0Id = list.get(n);
						}else{
							supplyArea0Id = supplyArea0Id + "," + list.get(n);
						}
					}
					params.put("supplyArea0Id", supplyArea0Id );
					productFreightRulesMapper.deleteShopFreightRules(params);
					productFreightRulesAreaMapper.deleteAreaFreightRules(params);
				}
			}
		}*/
		
		//店铺运费规则插入数据
		params.put("ls", ls);
		//店铺区域运费规则插入数据
		params.put("areals", areals);
		Boolean status= productFreightRulesMapper.insertNewShopFreightRules(params)>0&&productFreightRulesAreaMapper.insertAreaFreightRules(params)>0?true:false;
		
		return status;
		
	}
	
	
	@Transactional(value = "txManager", isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public Boolean deleteShopFreightRules(List<ProductFreightRulesVo> ls) {
		//查询店铺信息
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			for(int i=0;i<ls.size();i++){
				if(!"".equals(ls.get(i).getAreaids())){
					params.put("supplyArea0Id", ls.get(i).getSupplyArea0Id());
					productFreightRulesMapper.deleteShopFreightRules(params);//删除已有的运费规则
					productFreightRulesAreaMapper.deleteAreaFreightRules(params);//删除已有的运费规则
				}
			
			}
		
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	@Override
	public List<List<ShopFreightRulesVo>> getShopFreightRules(
			List<FreightConditionVo> voList) {
		List<List<ShopFreightRulesVo>> resultList = new ArrayList<List<ShopFreightRulesVo>>();
		Map<String, Object> params = new HashMap<String, Object>();
		
		for (FreightConditionVo freightConditionVo : voList) {
			params.put("shopId", freightConditionVo.getShop_id());
			params.put("supplyAreaId", freightConditionVo.getSupply_area_id());
			
			//根据产品发布的id查询单品运费规则
			List<ShopFreightRulesVo> pfrList = productFreightRulesMapper.findProductFreightRulesBysupplyArea0Id(params);
			List<ShopFreightRulesVo> resultli = new ArrayList<ShopFreightRulesVo>();
			//判断单品运费是否存在
			if(pfrList.size() > 0){
				for (ShopFreightRulesVo shopFreightRulesVo : pfrList) {
					shopFreightRulesVo.setShopId(freightConditionVo.getShop_id());
				}
				  params.put("status", "1");
				 //查询地区运费规则并进行数据处理
				  resultList.add(this.getShopRulesDetils(pfrList, resultli, params));
			}else{
				params.put("status", "");
				params.put("rulesType", freightConditionVo.getType());
				List<ShopFreightRulesVo> nowlist = shopFreightRulesMapper.selectShopExpressCarrier(params);
				resultList.add(getShopRulesDetils(nowlist, resultli,params));
			}
		}
			
		return resultList;
	}

	@Override
	public ProductSortVo findProductSortBySortId(String sortId) {
		return productSortMapper.findProductSortBySortId(sortId);
	}

	@Override
	public ShopFreightRulesVo getShopFreightRulesBySlyArea0Id(
			BigDecimal supplyArea0Id,BigDecimal areaId) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("supplyAreaId", supplyArea0Id);
		params.put("areaId", areaId);
		//根据产品发布的id查询店铺的id已经产品的类型
		ShopFreightRulesVo sfrVo = productFreightRulesMapper.findShopIdAndSortIdBySupplyArea0Id(params);
		params.put("shopId", sfrVo.getShopId());
		ShopFreightRulesVo pfr = new ShopFreightRulesVo();
		//根据发布id与承运地区id查询单品的运费规则
		 pfr = productFreightRulesMapper.findProductFreightRulesBysupplyArea0IdAndAreaId(params);
		//如果单品的运费规则为空则取店铺的运费规则
		if(pfr==null){
			params.put("rulesType",  commonService.getShopFreightRulesType(null, sfrVo.getSortId()));
			pfr = shopFreightRulesMapper.findShopFreightRulesByShopIdAndAreaId(params);
		}
		
		return pfr==null?new ShopFreightRulesVo():pfr;
	}
	
	@Override
	public List<ProductFreightRulesVo> getProductFreightRulesBySlyArea0Id(
			BigDecimal supplyArea0Id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("supplyAreaId", supplyArea0Id);
		//根据产品发布的id查询店铺的id已经产品的类型
		ShopFreightRulesVo sfrVo = productFreightRulesMapper.findShopIdAndSortIdBySupplyArea0Id(params);
		params.put("shopId", sfrVo.getShopId());
		//通过发布id查询单品的运费规则
		List<ProductFreightRulesVo> pfrList = productFreightRulesMapper.findProductFreightRulesBySArea0Id(params);
		if(pfrList.size() !=0){
			for (ProductFreightRulesVo productFreightRulesVo : pfrList) {
				productFreightRulesVo.setShopId(sfrVo.getShopId());
			}	
			
			 //查询地区运费规则并进行数据处理
			 for(int i=0;i<pfrList.size();i++){
					String freightid = StringHelperTools.nvl(pfrList.get(i).getId());
					params.put("freightid", freightid);
					if(!"".equals(freightid)){
						List<ProductFreightRulesAreaVo> arealist = productFreightRulesAreaMapper.selectAreaFreightRulesByFrId(params);
						if(arealist!=null && arealist.size()>0){
							pfrList.get(i).setAreaFreightList(arealist);
						}
					}
				}
		}
		
		return pfrList;
	}
	
	@Override
	public List<ShopFreightRulesVo> getShopFreightRulesBySlyArea0Id(
			BigDecimal supplyArea0Id) {
		
		List<ShopFreightRulesVo> resultList = new ArrayList<ShopFreightRulesVo>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("supplyAreaId", supplyArea0Id);
		//根据产品发布的id查询店铺的id已经产品的类型
		ShopFreightRulesVo sfrVo = productFreightRulesMapper.findShopIdAndSortIdBySupplyArea0Id(params);
		params.put("shopId", sfrVo.getShopId());
		//通过发布id查询单品的运费规则
		List<ShopFreightRulesVo> pfrList = productFreightRulesMapper.findProductFreightRulesBysupplyArea0Id(params);
		List<ShopFreightRulesVo> resultli = new ArrayList<ShopFreightRulesVo>();
		
		//判断单品运费是否存在
		if(pfrList.size() > 0){
			for (ShopFreightRulesVo shopFreightRulesVo : pfrList) {
				shopFreightRulesVo.setShopId(sfrVo.getShopId());
			}
			  params.put("status", "1");
			 //查询地区运费规则并进行数据处理
			  resultList =this.getShopRulesDetils(pfrList, resultli, params);
		}else{
			params.put("status", "");
			params.put("rulesType",  commonService.getShopFreightRulesType(null, sfrVo.getSortId()));
			List<ShopFreightRulesVo> nowlist = shopFreightRulesMapper.selectShopExpressCarrier(params);
			resultList=getShopRulesDetils(nowlist, resultli,params);
		}
		
		return resultList;
	}

	@Override
	public ProductSortVo findProductSortByProductId(String productId) {
		
		return productSortMapper.findProductSortByProductId(productId);
	}


	@Override
	public List<AreaFreightRulesVo> findFreoghtRulesAreaByAreaIds(
			String[] areaId) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("areaIds", areaId);
		return areaFreightRulesMapper.findFreoghtRulesAreaByAreaId(map);
	}

	@Override
	public ShopFreightRulesVo getShopFreightRulesBySlyArea0IdAndAId(
			BigDecimal supplyArea0Id, BigDecimal areaId) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("supplyAreaId", supplyArea0Id);
		params.put("areaId", areaId);
		//根据产品发布的id查询店铺的id已经产品的类型
		ShopFreightRulesVo sfrVo = productFreightRulesMapper.findShopIdAndSortIdBySupplyArea0Id(params);
		params.put("shopId", sfrVo.getShopId());
		
		//如果单品的运费规则为空则取店铺的运费规则
		params.put("rulesType",  commonService.getShopFreightRulesType(null, sfrVo.getSortId()));
		ShopFreightRulesVo	pfr = shopFreightRulesMapper.findShopFreightRulesByShopIdAndAreaId(params);
		
		return pfr;
	}

	@Override
	public ProductFreightRulesVo getProductFreightRulesBySlyArea0IdAndAId(
			BigDecimal supplyArea0Id, BigDecimal areaId) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("supplyAreaId", supplyArea0Id);
		params.put("areaId", areaId);
		//根据发布id与承运地区id查询单品的运费规则
		ProductFreightRulesVo  pfr = productFreightRulesMapper.findProductFreightRulesBysupplyArea0IdAndAId(params);
		
		return pfr;
	
	}

	@Override
	public ShopFreightRulesVo getShopFreightRulesBySlyA0Id(BigDecimal supplyArea0Id,
			BigDecimal areaId) {

		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("supplyAreaId", supplyArea0Id);
		params.put("areaId", areaId);
		//根据产品发布的id查询店铺的id已经产品的类型
		ShopFreightRulesVo sfrVo = productFreightRulesMapper.findShopIdAndSortIdBySupplyArea0Id(params);
		params.put("shopId", sfrVo.getShopId());
		ShopFreightRulesVo pfr = new ShopFreightRulesVo();
		//根据发布id与承运地区id查询单品的运费规则
		 pfr = productFreightRulesMapper.findProductFreightRulesBysupplyArea0IdAndAreaId(params);
		//如果单品的运费规则为空则取店铺的运费规则
		if(pfr==null){
			params.put("rulesType",  commonService.getShopFreightRulesType(null, sfrVo.getSortId()));
			pfr = shopFreightRulesMapper.findShopFreightRulesByShopIdAndAreaId(params);
		}
		
		return pfr==null?new ShopFreightRulesVo():pfr;
	
	}
	

	@Override
	public ShopFreightRulesVo getFreightDetailById(BigDecimal id ,BigDecimal areaId,String userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userid",userId);
		ShopFreightRulesVo sfrvo = shopFreightRulesMapper.selectConsAddrByUserId(params);
		String consigneeaddress = sfrvo.getConsigneeaddress();
		String provinceId = sfrvo.getProvinceId();
		params.put("areaid",provinceId);
		String province = shopFreightRulesMapper.findAreaNameById(params);
		String cityId = sfrvo.getCityId();
		params.put("areaid",cityId);
		String city = shopFreightRulesMapper.findAreaNameById(params);
		String countyId = sfrvo.getAreaId();
		String countyname ="";
		if(!cityId.equals(countyId)){
			params.put("areaid",countyId);
			countyname = shopFreightRulesMapper.findAreaNameById(params);
		}
		if(consigneeaddress == null || consigneeaddress == ""){
			 sfrvo.setStr("请先设置默认收货地址！");
			 return sfrvo;
		}else{
			ShopFreightRulesVo vo = getShopFreightRulesBySlyA0Id(id,areaId);
			vo.setStr(province + city + countyname + consigneeaddress);
			return vo;
		}	
	}


	

}
